package at.ac.univie.zsu.aguataplan.domain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Version;
import javax.validation.constraints.Size;

/**
 * @author gerry
 * 
 */
@Entity
public class Event {

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	@Column
	private Long id;

	@Version
	@Column
	private Integer version;

	@Column(nullable = false, unique = true)
	@Size(min = 1, max = 30)
	private String name;

	@Column
	private String description;

	@Column
	@Size(min = 0, max = 1)
	private String shortcut;

	@OneToMany(cascade = CascadeType.ALL)
	private Set<DetectedEvent> refDetectedEvent = new HashSet<DetectedEvent>();

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private Set<EventAttribute> refEventAttribute = new HashSet<EventAttribute>();

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public Event() {

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	public boolean addEventAttribute(EventAttribute eventAttribute) {
		return this.refEventAttribute.add(eventAttribute);
	}

	public boolean deleteEventAttribute(EventAttribute eventAttribute) {
		Iterator<EventAttribute> it = refEventAttribute.iterator();

		long i;
		i = eventAttribute.getId();
		long j = -1;

		while (it.hasNext()) {
			EventAttribute e = it.next();
			j = e.getId();
			if (i == j) {
				it.remove();
				return true;
			}
		}

		return false;
	}

	public void cleanEventAttribute() {
		this.refEventAttribute = new HashSet<EventAttribute>();
	}

	public String toString() {
		return this.name;
	}

	public String toStringExtended() {
		StringBuilder sb = new StringBuilder();
		sb.append("Description: ").append(getDescription()).append(", ");
		sb.append("Id: ").append(getId()).append(", ");
		sb.append("Name: ").append(getName()).append(", ");
		sb.append("RefAttribute: ").append(getRefEventAttribute() == null ? "null" : getRefEventAttribute().size())
				.append(", ");
		sb.append("RefDetectedEvent: ").append(getRefDetectedEvent() == null ? "null" : getRefDetectedEvent().size())
				.append(", ");
		sb.append("Shortcut: ").append(getShortcut()).append(", ");
		sb.append("Version: ").append(getVersion());
		return sb.toString();
	}

	public String toStringExport() {
		return this.name;
	}

	public static String[] toStringExportHeadline() {
		List<String> list = new ArrayList<String>();
		list.add("Event");
		list.add("EventAttribute");
		list.add("EventAttributeValue");
		return list.toArray(new String[] {});
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getShortcut() {
		return shortcut;
	}

	public void setShortcut(String shortcut) {
		this.shortcut = shortcut;
	}

	public Set<DetectedEvent> getRefDetectedEvent() {
		return refDetectedEvent;
	}

	public void setRefDetectedEvent(Set<DetectedEvent> refDetectedEvent) {
		this.refDetectedEvent = refDetectedEvent;
	}

	public Set<EventAttribute> getRefEventAttribute() {
		return refEventAttribute;
	}

	public void setRefEventAttribute(Set<EventAttribute> refEventAttribute) {
		this.refEventAttribute = refEventAttribute;
	}

}
